<?php


namespace app\controller;


use app\model\Faces;
use app\model\MqttUserManage;
use app\model\MqttFacePushInfo;
use think\facade\Db;

class Sta extends BackendController{
    protected $model = null;
    public function _initialize()
    {
        //parent::_initialize();
        $this->model = new \app\model\Push();
    }

    public function faces(){
        $face_count=Faces::count();
        $mtqq_count=MqttUserManage::count();
        $mqtt_push_count=MqttFacePushInfo::count();
        $range_face=Db::name('faces')->group('aizone_id')->field('aizone_id,aizone_name,count(*) co')->order('co desc')->select();
        $range_mqtt=Db::name('mqtt_face_push_info')->group('aizone_id')->field('aizone_id,aizone_name,count(*) co')->order('co desc')->select();
        $this->success('获取成功',['face_count'=>$face_count,'mqtt_count'=>$mtqq_count,'mqtt_push_count'=>$mqtt_push_count,'face_range'=>$range_face,'mqtt_range'=>$range_mqtt]);
    }

    public function aizone($aizone_id='',$start_time='',$end_time=''){
        $where=[];
        if($aizone_id){
            $where[]=['aizone_id','=',$aizone_id];
        }

        if($start_time){
            $where[]=['create_time','>',$start_time];
        }

        if($end_time){
            $where[]=['create_time','<',$end_time];
        }
        //$res['face_week']=Db::name('faces')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,week(create_time) `week`,count(*) `count`')->order('year desc,week desc')->select();
        //$res['face_year']=Db::name('faces')->where($where)->group('year(create_time)')->field('year(create_time) `year`,count(*) `count`')->order('year desc')->select();
        $res['face']=Db::name('faces')->where($where)->group('year(create_time),month(create_time),day(create_time)')->field('year(create_time) year,month(create_time) `month`,day(create_time) `day`,count(*) `count`')->order('year desc,month desc,day desc')->select();
        //$res['mqtt_week']=Db::name('mqtt_face_push_info')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,week(create_time) `week`,count(*) `count`')->order('year desc,week desc')->select();
        $res['mqtt_year']=Db::name('mqtt_face_push_info')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,count(*) `count`')->order('year desc')->select();
        $this->success('获取成功',$res);

    }

    public function aizone_day($aizone_id='',$start_time='',$end_time=''){
        $where=[];
        if($aizone_id){
            $where[]=['aizone_id','=',$aizone_id];
        }
        if($start_time){
            $where[]=['create_time','>',$start_time];
        }

        if($end_time){
            $where[]=['create_time','<',$end_time];
        }else{
            $end_time=date('Y-m-d');
        }
        //$res['face_week']=Db::name('faces')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,week(create_time) `week`,count(*) `count`')->order('year desc,week desc')->select();
        //$res['face_year']=Db::name('faces')->where($where)->group('year(create_time)')->field('year(create_time) `year`,count(*) `count`')->order('year desc')->select();
        $res=Db::name('faces')->where($where)->group('month(create_time),day(create_time)')->field('month(create_time) `month`,day(create_time) `day`,count(*) `count`')->order('month asx,day asc')->select();
        //$res['mqtt_week']=Db::name('mqtt_face_push_info')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,week(create_time) `week`,count(*) `count`')->order('year desc,week desc')->select();
        $results=[];
        for ($i=0 ;$i<999;$i++) {
            $day_time=strtotime("+".$i." days",strtotime($start_time));
            if($day_time>strtotime($end_time)){
                break;
            }
            $month=date('m',$day_time);
            $day=date('j',$day_time);
            $flag=false;
            foreach ($res as $re){
                if((int)($re['month'])==(int)($month)&&(int)($re['day'])==(int)($day)){
                    $results[]=$re;
                    $flag=true;
                    break;
                }
            }
            if(!$flag){
                $results[]=['month'=>$month,'day'=>$day,'count'=>0];
            }
        }
        $this->success('获取成功',$results);

    }

    public function aizone_month($aizone_id='',$start_time='',$end_time=''){
        $where=[];
        if($aizone_id){
            $where[]=['aizone_id','=',$aizone_id];
        }
        if($start_time){
            $where[]=['create_time','>',$start_time];
        }

        if($end_time){
            $where[]=['create_time','<',$end_time];
        }else{
            $end_time=date('Y-m-d');
        }
        //$res['face_week']=Db::name('faces')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,week(create_time) `week`,count(*) `count`')->order('year desc,week desc')->select();
        //$res['face_year']=Db::name('faces')->where($where)->group('year(create_time)')->field('year(create_time) `year`,count(*) `count`')->order('year desc')->select();
        $res=Db::name('faces')->where($where)->group('month(create_time),year(create_time)')->field('month(create_time) `month`,year(create_time) `year`,count(*) `count`')->order('year asc,month asc')->select();
        //$res['mqtt_week']=Db::name('mqtt_face_push_info')->where($where)->group('year(create_time),week(create_time)')->field('year(create_time) `year`,week(create_time) `week`,count(*) `count`')->order('year desc,week desc')->select();
        $results=[];
        for ($i=0 ;$i<99;$i++) {
            $day_time=strtotime("+".$i." months",strtotime($start_time));
            if($day_time>strtotime($end_time)){
                break;
            }
            $month=date('n',$day_time);
            $year=date('Y',$day_time);
            $flag=false;
            foreach ($res as $re){
                if((int)($re['month'])==(int)($month)&&(int)($re['year'])==(int)($year)){
                    $results[]=$re;
                    $flag=true;
                    break;
                }
            }
            if(!$flag){
                $results[]=['month'=>$month,'year'=>$year,'count'=>0];
            }
        }
        $this->success('获取成功',$results);
    }

}